$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'xk/course/list',
        datatype: "json",
        colModel: [			
			{ label: '课程ID', name: 'courseId', index: "course_id", width: 45, key: true },
			{ label: '课程名称', name: 'courseName', width: 75 },
            { label: '课程学分', name: 'courseRedit', sortable: false, width: 75 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});
var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            courseName: null
        },
        showList: true,
        title:null,
        course:{
            courseId:null,
            courseName:null,
            courseRedit:null
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.course = {courseId:null,
                courseName:null,
                courseRedit:null};
        },
        update: function () {
            var courseId = getSelectedRow();
            if(courseId == null){
                return ;
            }

            vm.showList = false;
            vm.title = "修改";
            vm.getCourse(courseId);
        },
        del: function () {
            var courseIds = getSelectedRows();
            if(courseIds == null){
                return ;
            }

            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "xk/course/delete",
                    contentType: "application/json",
                    data: JSON.stringify(courseIds),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        saveOrUpdate: function () {
            var url = vm.course.courseId == null ? "xk/course/save" : "xk/course/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.course),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        getCourse: function(courseId){
            $.get(baseURL + "xk/course/info/"+courseId, function(r){
                vm.course = r.course;
            });
        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'username': vm.q.courseName},
                page:page
            }).trigger("reloadGrid");
        }
    }
});